package com.ease.framework.workbench.spi.impls {
	import com.ease.framework.workbench.Workbench;
	import com.ease.framework.workbench.spi.ICommand;
	
	
	/**
	 * The stack caches the command.  
	 * 
	 * @author James Hu
	 * 
	 */	
	public class CommandStack {
        private var undolist:Array;		//undo list
        private var redolist:Array;		//redo list
        private var _maxUndoLength:uint = 100;

        public function CommandStack() {
            undolist = [];
            redolist = [];
            return;
        }// end function

        public function add(param1:ICommand) : ICommand {
/*             if (!param1.isValid())
            {
                return null;
            }// end if */
            while (undolist.length > _maxUndoLength) {
                // label
                undolist.shift();
            }// end while
            undolist.push(param1);
            redolist.splice(0, redolist.length);
            return param1;
        }// end function

        public function redo() : Boolean {
            var _loc_1:* = redolist.pop();
            if (_loc_1) {
                undolist.push(_loc_1);
                _loc_1.execute();
                return redolist.length > 0;
            }// end if
            return false;
        }// end function

        public function getLastUndo() : Command {
            if (undolist.length > 0) {
                return undolist[undolist.length - 1];
            }// end if
            return null;
        }// end function

        public function getLastRedo() : Command {
            if (redolist.length > 0) {
                return redolist[redolist.length - 1];
            }
            return null;
        }// end function

        public function undo() : Boolean {
            var _loc_1:* = undolist.pop();
            if (_loc_1) {
                while (redolist.length > _maxUndoLength) {
                    // label
                    redolist.shift();
                }// end while
                redolist.push(_loc_1);
                _loc_1.undo();
                return undolist.length > 0;
            }// end if
            return false;
        }// end function

        public function flush() : void {
            flushRedo();
            flushUndo();
        }
        
        public function flushRedo():void{
        	   redolist.splice(0, redolist.length);
        }
        
        public function flushUndo():void{
        	   undolist.splice(0, undolist.length);
        }

        public function get maxUndoLength() : uint {
            return _maxUndoLength;
        }// end function

        public function get canUndo() : Boolean {
            return undolist.length > 0;
        }// end function

        public function get canRedo() : Boolean {
            return redolist.length > 0;
        }// end function

        public function push(param1:ICommand) : void {
            if (param1) {
                add(param1).execute();
                Workbench.instance.isDirty = true;
            }// end if
            return;
        }// end function

        public function set maxUndoLength(param1:uint) : void {
            _maxUndoLength = Math.min(param1, 100);
            return;
        }// end function
	}
}